package com.kayang.wechat.controller;

import com.kayang.common.HttpMsg;
import com.kayang.nadapt.util.StringUtil;
import com.kayang.wechat.config.WeChatConfig;
import com.kayang.wechat.pojo.msg.*;
import com.kayang.wechat.service.AccessTokenService;
import com.kayang.wechat.pojo.WechatContext;
import com.kayang.wechat.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping(value = "/message")
public class MessageController extends BaseController {

    @Autowired
    AccessTokenService tokenService;

    @Autowired
    WeChatConfig weChatConfig;

    @Autowired
    MessageService messageService;

    //文本消息
    @PostMapping("/sendText")
    public ResponseEntity sendText(@Valid @RequestBody TextMsg textMsg){
        HttpMsg httpMsg = sendAll(textMsg);
        return ResponseEntity.ok(httpMsg);
    }

    //图片消息
    @PostMapping("/sendImage")
    public ResponseEntity sendImage(@Valid @RequestBody ImageMsg imageMsg){
        HttpMsg httpMsg = sendAll(imageMsg);
        return ResponseEntity.ok(httpMsg);
    }

    //语音消息
    @PostMapping("/sendVoice")
    public ResponseEntity sendVoice(@Valid @RequestBody VoiceMsg voiceMsg){
        HttpMsg httpMsg = sendAll(voiceMsg);
        return ResponseEntity.ok(httpMsg);
    }

    //视频消息
    @PostMapping("/sendVideo")
    public ResponseEntity sendVideo(@Valid @RequestBody VideoMsg videoMsg){
        HttpMsg httpMsg = sendAll(videoMsg);
        return ResponseEntity.ok(httpMsg);
    }


    //文件消息
    @PostMapping("/sendFile")
    public ResponseEntity sendFile(@Valid @RequestBody FileMsg fileMsg){
        HttpMsg httpMsg = sendAll(fileMsg);
        return ResponseEntity.ok(httpMsg);
    }

    //文件卡片
    @PostMapping("/sendTextcard")
    public ResponseEntity sendTextcard(@Valid @RequestBody CardMsg cardMsg){
        HttpMsg httpMsg = sendAll(cardMsg);
        return ResponseEntity.ok(httpMsg);
    }

    //图文消息
    @PostMapping("/sendNews")
    public ResponseEntity sendNews(@Valid @RequestBody NewsMsg newsMsg){
        HttpMsg httpMsg = sendAll(newsMsg);
        return ResponseEntity.ok(httpMsg);
    }

    //图文消息2
    @PostMapping("/sendMPNews")
    public ResponseEntity sendMPNews(@Valid @RequestBody MPNewsMsg mpnewsMsg){
        HttpMsg httpMsg = sendAll(mpnewsMsg);
        return ResponseEntity.ok(httpMsg);
    }

    //markdown消息
    @PostMapping("/sendMarkdown")
    public ResponseEntity sendMarkdown(@Valid @RequestBody MarkdownMsg markdownMsg){
        HttpMsg httpMsg = sendAll(markdownMsg);
        return ResponseEntity.ok(httpMsg);
    }

    //小程序通知消息  miniprogram_notice
    @PostMapping("/sendMiniprogramNotice")
    public ResponseEntity sendMiniprogramNotice(@Valid @RequestBody MiniprogramNoticeMsg miniprogramNoticeMsg){
        HttpMsg httpMsg = sendAll(miniprogramNoticeMsg);
        return ResponseEntity.ok(httpMsg);
    }


    private HttpMsg sendAll(BaseMsg baseMsg){
        HttpMsg httpMsg = new HttpMsg();
        if(StringUtil.isNullOrEmpty(baseMsg.getTouser()) && StringUtil.isNullOrEmpty(baseMsg.getToparty()) && StringUtil.isNullOrEmpty(baseMsg.getTotag())){
            httpMsg.setMsgId(-1);
            httpMsg.setMsg("touser, toparty, totag不能同时为空");
            return httpMsg;
        }

        String url = weChatConfig.getPath() + "/cgi-bin/message/send?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getAccessTokenStr(weChatConfig.getAppKey()));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(baseMsg.toMap(baseMsg));
        Map ret = messageService.send(baseMsg, context);

        httpMsg.setMsgId(0);
        httpMsg.setMsg("发送成功");
        httpMsg.setResult(ret);

        return httpMsg;
    }
}
